package com.cndongtai.interceptor;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.cndongtai.constant.SystemConstant;

public class WeixinSessionInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		
		checkLanguage(request);
		
		String uri = request.getRequestURI();
		if(uri.contains("/torchn/login.do") || uri.contains("/torchn/saveInverterCollect.do") || uri.contains("/torchn/saveQuantity.do") 
				|| uri.contains("/torchn/queryChargeQuantity.do") || uri.contains("/torchn/queryWorkQuantity.do")) {
			return true;
		}
		
		return super.preHandle(request, response, handler);
	}
	
	private void checkLanguage(HttpServletRequest request) {
		String language = (String) request.getSession().getAttribute(SystemConstant.LANGUAGE);
		if(language==null || "".equals(language)) {
			request.getSession().setAttribute(SystemConstant.LANGUAGE, Locale.getDefault().getLanguage());
		}
		
		if("en".equals(request.getParameter("language"))) {
			request.getSession().setAttribute(SystemConstant.LANGUAGE, "en");
		}else if("zh".equals(request.getParameter("language"))) {
			request.getSession().setAttribute(SystemConstant.LANGUAGE, "zh");
		}
	}

}
